﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using System.Net;
using System.Collections;
using System.Xml;
using System.Reflection;
using System.Web;
using System.Diagnostics;

namespace SlideShow
{
    class Dropbox//useless now
    {
        public void dropbox_author()
        {
            var consumerKey = "56mrkw3ejdx2ucz";
            var consumerSecret = "b8lm9tiecf1goav";
            var uri = new Uri("https://api.dropbox.com/1/oauth/request_token");

            OAuthBase oAuth = new OAuthBase();
            string nonce = oAuth.GenerateNonce();
            string timeStamp = oAuth.GenerateTimeStamp();
            string parameters;
            string normalizedUrl;
            string signature = oAuth.GenerateSignature(uri, consumerKey, consumerSecret, String.Empty, String.Empty, "GET", timeStamp, nonce,
                OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out parameters);
            signature = HttpUtility.UrlEncode(signature);

            StringBuilder requestUri = new StringBuilder(uri.ToString());
            requestUri.AppendFormat("?oauth_consumer_key={0}&", consumerKey);
            requestUri.AppendFormat("oauth_nonce={0}&", nonce);
            requestUri.AppendFormat("oauth_timestamp={0}&", timeStamp);
            requestUri.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
            requestUri.AppendFormat("oauth_version={0}&", "1.0");
            requestUri.AppendFormat("oauth_signature={0}", signature);

            var request = (HttpWebRequest)WebRequest.Create(new Uri(requestUri.ToString()));
            request.Method = WebRequestMethods.Http.Get;

            var response = request.GetResponse();
            var queryString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            var parts = queryString.Split('&');
            var token = parts[1].Substring(parts[1].IndexOf('=') + 1);
            var tokenSecret = parts[0].Substring(parts[0].IndexOf('=') + 1);

            var queryString1 = String.Format("oauth_token={0}", token);
            var authorizeUrl = "https://www.dropbox.com/1/oauth/authorize?" + queryString;
            Process.Start(authorizeUrl);
            System.Threading.Thread.Sleep(10000);
            dropbox_access(token, tokenSecret);
        }
        public void dropbox_access(string token, string tokenSecret)
        {
            var consumerKey = "56mrkw3ejdx2ucz";
            var consumerSecret = "b8lm9tiecf1goav";
            var uri = "https://api.dropbox.com/1/oauth/access_token";

            OAuthBase oAuth = new OAuthBase();
            var nonce = oAuth.GenerateNonce();
            var timeStamp = oAuth.GenerateTimeStamp();
            string parameters;
            string normalizedUrl;
            var signature = oAuth.GenerateSignature(new Uri(uri), consumerKey, consumerSecret,
                token, tokenSecret, "GET", timeStamp, nonce,
                OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out parameters);

            signature = HttpUtility.UrlEncode(signature);
            var requestUri = new StringBuilder(uri);
            requestUri.AppendFormat("?oauth_consumer_key={0}&", consumerKey);
            requestUri.AppendFormat("oauth_token={0}&", token);
            requestUri.AppendFormat("oauth_nonce={0}&", nonce);
            requestUri.AppendFormat("oauth_timestamp={0}&", timeStamp);
            requestUri.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
            requestUri.AppendFormat("oauth_version={0}&", "1.0");
            requestUri.AppendFormat("oauth_signature={0}", signature);

            var request = (HttpWebRequest)WebRequest.Create(requestUri.ToString());
            request.Method = WebRequestMethods.Http.Get;

            var response = request.GetResponse();
            var reader = new StreamReader(response.GetResponseStream());
            var accessToken = reader.ReadToEnd();

            var parts = accessToken.Split('&');
            var Token = parts[1].Substring(parts[1].IndexOf('=') + 1);
            var Secret = parts[0].Substring(parts[0].IndexOf('=') + 1);

            OAuthToken accessTokenObject = new OAuthToken(Token, Secret);


            var api = new DropboxApi(consumerKey, consumerSecret, accessTokenObject);
            var account = api.GetAccountInfo();
            Console.WriteLine(Token);
            Console.WriteLine(Secret);
            Console.WriteLine(account);

            var publicFolder = api.GetFiles("dropbox", "");
            foreach (var file in publicFolder.Contents)
            {
                Console.WriteLine(file.Path);
                Console.WriteLine(file.Modified);
                var files = api.DownloadFile("dropbox", file.Path);
                files.Save(@"C:\DropboxPart3.zip");
                
            }
        }
    }
}
